package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.bean.vo.Order;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.IRouteServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RoutePageAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteAResp;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RoutePageAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.constant.CommonConstant;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.Route;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.RouteQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.RouteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;

@RestController
public class RouteServiceRemoteImpl implements IRouteServiceRemote {
    @Autowired
    private RouteService routeService;

    @Override
    public ResponseData<RoutePageAResp> list(RoutePageAReq routePageAReq) {
        ResponseData<RoutePageAResp> responseData = new ResponseData<>();
        if (routePageAReq.getPage() == 0) {
            routePageAReq.setPage(1);
        }
        if (routePageAReq.getSize() == 0) {
            routePageAReq.setSize(Integer.MAX_VALUE);//默认查询全部
        }

        Pagination<RouteEnhanced> pagination = new Pagination<>(routePageAReq.getPage(), routePageAReq.getSize(), true);
        pagination.addOrder(new Order(Route.COLUMN_STATUS, Order.ORDER_DESC), Route.class)
                .addOrder(new Order(Route.COLUMN_CREATE_TIME, Order.ORDER_DESC), Route.class);

        RouteQuery routeQuery = new RouteQuery();
        routeQuery.setApplicationNames(Lists.newArrayList(routePageAReq.getApplicationName()));
        routeQuery.setLikeId(routePageAReq.getLikeId());
        routeQuery.setLikePath(routePageAReq.getLikePath());
        routeQuery.setLikeServiceId(routePageAReq.getLikeServiceId());
        routeQuery.setLikeDomain(routePageAReq.getLikeDomain());
        routeQuery.setNotInServiceIds(Lists.newArrayList(CommonConstant.DEFAULT_SERVICE_ID));
        if (Detect.notEmpty(routePageAReq.getLikeComment())) {
            routeQuery.setLikeComment(URLDecoder.decode(routePageAReq.getLikeComment()));
        }


        pagination = routeService.getPagination(routeQuery, pagination, null);

        RoutePageAResp routePageAResp = new RoutePageAResp();
        routePageAResp.setCount(pagination.getCount());
        routePageAResp.setPage(pagination.getIndex());
        routePageAResp.setSize(pagination.getSize());

        if (Detect.notEmpty(pagination.getItems())) {
            routePageAResp.setItems(CommonConvent.convent2RouteAResps(pagination.getItems()));
        }

        responseData.setData(routePageAResp);

        return responseData;
    }

    @Override
    public ResponseData<RouteAResp> detail(@PathVariable("id") String id) {
        ResponseData<RouteAResp> responseData = new ResponseData<>();

        RouteEnhanced routeEnhanced = routeService.getEnhanced(id, null);

        responseData.setData(CommonConvent.convent2RouteAResp(routeEnhanced));

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<RouteUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();
        RouteUpdateAReq routeUpdateAReq = requestData.getRequest();

        routeService.updateRoute(routeUpdateAReq.getId(), routeUpdateAReq.getPath(), routeUpdateAReq.getServiceId(),
                routeUpdateAReq.getStripPrefix(), routeUpdateAReq.getAddPrefix(), routeUpdateAReq.getSensitiveHeaders(), routeUpdateAReq.getAddHostHeader(),
                routeUpdateAReq.getStatus(), routeUpdateAReq.getSort(), routeUpdateAReq.getDomainRouteId(), routeUpdateAReq.getComment(), null);

        return responseData;
    }

    @Override
    public ResponseData<RouteAResp> add(@RequestBody RequestData<RouteAddAReq> requestData) {
        ResponseData<RouteAResp> responseData = new ResponseData<>();

        Route route = new Route();
        BeanUtils.copyProperties(requestData.getRequest(), route);

        route = routeService.addRoute(route, null);

        responseData.setData(CommonConvent.convent2RouteAResp(routeService.convent2Enhanced(route)));

        return responseData;
    }

    @Override
    public ResponseData delete(@PathVariable("id") String id) {
        ResponseData responseData = new ResponseData();

        routeService.deleteRoute(id, null);

        return responseData;
    }
}
